package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import model.Xiaoy;
import util.Pagination;
import db.DBConnect;

public class XiaoyControl {
	public static Pagination pageInstance = null;
	
	/**
	 * 增加
	 * @author dujuan
	 * @return
	 * 2012-02-23
	 */
	public boolean insertXiaoy(Xiaoy xiaoy)
	{
		DBConnect dbc = new DBConnect();
		String sql ="insert into xiaoy (jingpName,jingptu,jingpNeirong) values (?,?,?)";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, xiaoy.getJingpName());
			dbc.setString(2,xiaoy.getJingptu());
			dbc.setString(3, xiaoy.getJingpNeirong());
					
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			dbc.close();
		}
	}
	
	/*判断数据库里面是否存在用户*/
	public static boolean isXiaoy(String a,String b,String c)
	{
		DBConnect dbc = new DBConnect();
		String sql ="select * from xiaoy where jingpname = ?  and jingptu=? and jingpNeirong=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1,a);
			dbc.setString(2,b);
			dbc.setString(3,c);
			ResultSet rs= dbc.executeQuery();
			if(rs.next()){
				return true;
			}
			else{
				return false;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			dbc.close();
		}return false;
	}
	
	
	/**
	 * 根据id获取信息
	 * @author 
	 * 2012-02-23
	 */
	
	public Xiaoy GetXiaoy(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from xiaoy where jingpId = ?";

		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			ResultSet rs = dbc.executeQuery();
			Xiaoy xiaoy = new Xiaoy();
			
			if(rs.next())
			{
				xiaoy.setJingpName(rs.getString("jingpName"));
				xiaoy.setJingptu(rs.getString("jingptu"));
				xiaoy.setJingpNeirong(rs.getString("jingpNeirong"));
						
				return xiaoy;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @author 
	 * 2012-02-23
	 */
	
	public boolean deleteXiaoy(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "delete from xiaoy where jingpId =?";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
		
	}
	
	/**
	 * 修改
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean updateXiaoy (Xiaoy xiaoy, int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "update xiaoy set jingpName=?, jingptu=? ,jingpNeirong=? where jingpId =?";
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, xiaoy.getJingpName());
			dbc.setString(2, xiaoy.getJingptu());
			dbc.setString(3, xiaoy.getJingpNeirong());
			dbc.setInt(4, id);
			
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	
	/**
	 * 列表返回显示总结
	 * 
	 */
	
	public Collection GetXiaoy()
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from xiaoy order by jingpId desc";
		dbc.prepareStatement(sql);
		Collection<Xiaoy> xiaoyList = new ArrayList<Xiaoy>();
		try {
			ResultSet rs = dbc.executeQuery();
			while(rs.next())
			{			
				Xiaoy xiaoy = new Xiaoy();
				xiaoy.setJingpId(rs.getInt("jingpId"));
				xiaoy.setJingpName(rs.getString("jingpName"));
				xiaoy.setJingptu(rs.getString("jingptu"));
				xiaoy.setJingpNeirong(rs.getString("jingpNeirong"));
			}
			return xiaoyList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		
		return null;
	}
	


	/*
	 * 返回总结列表，分页显示
	 */
	
	public Collection GetXiaoy(int page, int rowsPerPage)
	{
		String sql = "select * from xiaoy order by jingpId desc";
		pageInstance = new XiaoyControlPage(sql,rowsPerPage);
		
		try {
			return pageInstance.getPage(page);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}
	

class XiaoyControlPage extends Pagination{
	public XiaoyControlPage(String sql, int rowsPerPage)
	{
		super(sql,rowsPerPage,1);
	}
	
	@Override
	protected Collection<Xiaoy> packResultSet(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		Collection<Xiaoy>xiaoyList = new ArrayList<Xiaoy>();
		while(rs.next())
		{
			Xiaoy xiaoy = new Xiaoy();
			
			xiaoy.setJingpId(rs.getInt("jingpId"));	
			xiaoy.setJingpName(rs.getString("jingpName"));
			xiaoy.setJingptu(rs.getString("jingptu"));
			xiaoy.setJingpNeirong(rs.getString("jingpNeirong"));
			
			xiaoyList.add( xiaoy);
			xiaoy = null;
			}
			return xiaoyList;
			
		}
	
	}
	
}
	


	
	
	

	

	
	
